package controller;

import entity.EmpManager;
import service.EmpManagerService;
import service.EmpManagerServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "EmpManagerLoginCtrl", value = "/Manager/LoginCtrl")
public class EmpManagerLoginCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String inputVcode = request.getParameter("inputVcode");

        System.out.println("==========================================");
        String codes = (String) request.getSession().getAttribute("codes");

        if(!inputVcode.isEmpty()&&codes.equalsIgnoreCase(inputVcode)){
            EmpManagerService empManagerService = new EmpManagerServiceImpl();
            EmpManager empManager = empManagerService.login(username,password);
            if(empManager!=null){
                System.out.println(empManager);
                HttpSession session = request.getSession();
                session.setAttribute("empManager",empManager);
                response.sendRedirect(request.getContextPath()+"/Manager/safe/ShowAllEmpCtrl");
            }else {
                response.sendRedirect(request.getContextPath()+"/login.html");
            }
        }else {
            response.sendRedirect(request.getContextPath()+"/login.html");
        }
    }
}
